
package com.hexinfo.dmpro.script.config.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hexinfo.dmpro.common.base.PageResult;
import com.hexinfo.dmpro.common.utils.CommonConstants;
import com.hexinfo.dmpro.script.config.model.ConfigPropertyParam;
import com.hexinfo.dmpro.script.config.model.ConfigPropertyPublishuser;
import com.hexinfo.dmpro.script.config.dao.ConfigPropertyPublishuserMapper;
import com.hexinfo.dmpro.script.config.model.ConfigPropertyServer;
import com.hexinfo.dmpro.script.config.service.ConfigPropertyPublishuserService;
import com.hexinfo.dmpro.script.config.service.ConfigPropertyServerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

 /**
  * @Package com.hexinfo.dmpro.script.config.service.impl
  * @ClassName ConfigPropertyPublishuserServiceImpl
  * @Description 发布用户publish-user表
  * @Author le.zhang
  * @Date 2024/3/4 11:29
  * @Version v1.0
  **/
@Service
public class ConfigPropertyPublishuserServiceImpl extends ServiceImpl<ConfigPropertyPublishuserMapper, ConfigPropertyPublishuser> implements ConfigPropertyPublishuserService {
     @Autowired
        private ConfigPropertyServerService configPropertyServerService;



     /**
     * @Method queryList
     * @Param page
     * @Param configPropertyPublishuser
     * @Return com.hexinfo.dmpro.common.base.PageResult
     * @Description 分页查询
     * @Author le.zhang
     * @Date 2024/3/4 13:12
     * @Version V1.0
     */
    @Override
    public PageResult queryList(Page page, ConfigPropertyPublishuser configPropertyPublishuser) {
        LambdaQueryWrapper<ConfigPropertyPublishuser> wrapper = beforeQuery(configPropertyPublishuser);
        Page returnPage = this.baseMapper.selectPage(page, wrapper);
        List<ConfigPropertyPublishuser> configPropertyPublishuserList = returnPage.getRecords();
        afterQuery(returnPage.getRecords());
        return new PageResult(configPropertyPublishuserList, returnPage.getTotal());
    }

/**
 * @Method beforeQuery
 * @Param configPropertyPublishuser
 * @Return com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<com.hexinfo.dmpro.script.config.model.ConfigPropertyPublishuser>
 * @Description 分页前置查询，通过发布服务器标识精确查，权限用户名模糊查
 * @Author le.zhang
 * @Date 2024/3/4 13:12
 * @Version V1.0
 */
    private LambdaQueryWrapper<ConfigPropertyPublishuser> beforeQuery(ConfigPropertyPublishuser configPropertyPublishuser){
        LambdaQueryWrapper<ConfigPropertyPublishuser> wrapper = Wrappers.lambdaQuery();
        if (StrUtil.isNotBlank(configPropertyPublishuser.getServerId())) {
            wrapper.eq(ConfigPropertyPublishuser::getServerId, configPropertyPublishuser.getServerId());
        }
        if(StrUtil.isNotBlank(configPropertyPublishuser.getUsername())){
            wrapper.like(ConfigPropertyPublishuser::getUsername,configPropertyPublishuser.getUsername());
        }
        return wrapper;
    }

/**
 * @Method afterQuery
 * @Param configPropertyPublishuserList
 * @Return void
 * @Description 分页后置处理，将集群id转换为集群名称存入对象中
 * @Author le.zhang
 * @Date 2024/3/4 13:12
 * @Version V1.0
 */
    private void afterQuery(List<ConfigPropertyPublishuser> configPropertyPublishuserList) {
        for (ConfigPropertyPublishuser configPropertyPublishuser : configPropertyPublishuserList) {

            ConfigPropertyServer byId = configPropertyServerService.getById(configPropertyPublishuser.getServerId());
            if (byId != null){
                configPropertyPublishuser.setServerMark(byId.getMark());
            }

        }
    }

/**
 * @Method queryListNoPage
 * @Param configPropertyPublishuser
 * @Return java.util.Set<java.lang.String>
 * @Description 不分页查询，返回无重复的权限用户名的Set集合
 * @Author le.zhang
 * @Date 2024/3/4 13:12
 * @Version V1.0
 */

    @Override
    public Set<String> queryListNoPage(ConfigPropertyPublishuser configPropertyPublishuser) {
        LambdaQueryWrapper<ConfigPropertyPublishuser> wrapper = Wrappers.lambdaQuery();
        List<ConfigPropertyPublishuser> configPropertyPublishuserList = this.baseMapper.selectList(wrapper);

        Set publishuserSet = new HashSet();
        for (ConfigPropertyPublishuser publisher :
                configPropertyPublishuserList) {
            publishuserSet.add(publisher.getUsername());
        }
        return publishuserSet;
    }

/**
 * @Method findList
 * @Param configPropertyPublishuser
 * @Return java.util.List<com.hexinfo.dmpro.script.config.model.ConfigPropertyPublishuser>
 * @Description 不分页查所有，可通过集群id精确查
 * @Author le.zhang
 * @Date 2024/3/4 13:12
 * @Version V1.0
 */
    public List<ConfigPropertyPublishuser> findList(ConfigPropertyPublishuser configPropertyPublishuser) {
        LambdaQueryWrapper<ConfigPropertyPublishuser> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StrUtil.isNotBlank(configPropertyPublishuser.getServerId()), ConfigPropertyPublishuser::getServerId, configPropertyPublishuser.getServerId());
        List<ConfigPropertyPublishuser> configPropertyPublishuserList = this.baseMapper.selectList(wrapper);

        return configPropertyPublishuserList;
    }

/**
 * @Method findAll
 * @Param
 * @Return java.util.List<com.hexinfo.dmpro.script.config.model.ConfigPropertyPublishuser>
 * @Description json配置文件到数据库后获取所有数据
 * @Author le.zhang
 * @Date 2024/3/4 13:12
 * @Version V1.0
 */
    public List<ConfigPropertyPublishuser> findAll(){
        LambdaQueryWrapper<ConfigPropertyPublishuser> wrapper = Wrappers.lambdaQuery();
        List<ConfigPropertyPublishuser> configPropertyPublishuserList = this.baseMapper.selectList(wrapper);
        return configPropertyPublishuserList;
    }

    /**
     * @Method findUserByInfo
     * @Param
     * @param configPropertyPublishuser
     * @Return com.hexinfo.dmpro.script.config.model.ConfigPropertyPublishuser
     * @Description 查询用户信息
     * @Author Wang zhihao
     * @Date 2024/3/26 18:54
     * @Version V1.0
     */
     @Override
     public ConfigPropertyPublishuser findUserByInfo(ConfigPropertyPublishuser configPropertyPublishuser) {
         LambdaQueryWrapper<ConfigPropertyPublishuser> wrapper = Wrappers.lambdaQuery();
         wrapper.eq(StrUtil.isNotBlank(configPropertyPublishuser.getId()), ConfigPropertyPublishuser::getId, configPropertyPublishuser.getId());
         wrapper.eq(StrUtil.isNotBlank(configPropertyPublishuser.getUsername()), ConfigPropertyPublishuser::getUsername, configPropertyPublishuser.getUsername());
         wrapper.eq(StrUtil.isNotBlank(configPropertyPublishuser.getServerId()), ConfigPropertyPublishuser::getServerId, configPropertyPublishuser.getServerId());
         return this.baseMapper.selectOne(wrapper);
     }


}
